//
//  CustomTransitionAnimationViewController.m
//  动画
//
//  Created by 张国文 on 16/9/13.
//  Copyright © 2016年 李国峰. All rights reserved.
//

#import "CustomTransitionAnimationViewController.h"
#import "CustomPushAnimation.h"
#import "CustomPopAnimation.h"
#import "TargetViewController.h"
#import "CustomModelAnimaton.h"
#import "JiaoHuShiViewController.h"

@implementation CustomTransitionAnimationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.delegate = self;
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(10, 100, 300, 30);
    [button1 setTitle:@"自定义push动画" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(customAnimationWithTag:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 200;
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(10, 200, 300, 30);
    [button2 setTitle:@"自定义Model动画" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(customAnimationWithTag:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 201;
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(10, 300, 300, 30);
    [button3 setTitle:@"自定义交互式动画" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(customAnimationWithTag:) forControlEvents:UIControlEventTouchUpInside];
    button3.tag = 202;
    [self.view addSubview:button3];
     
    
}

- (void)customAnimationWithTag:(UIButton *)button
{
    switch (button.tag) {
        case 200:
        {
            TargetViewController *targetVC = [[TargetViewController alloc] init];
            [self.navigationController pushViewController:targetVC animated:YES];
            break;
        }
            case 201:
        {
            TargetViewController *targetVC = [[TargetViewController alloc] init];
            targetVC.transitioningDelegate = self;
            [self presentViewController:targetVC animated:YES completion:nil];
        }
            case 202:
        {
            JiaoHuShiViewController *jiaoHuShiVC = [[JiaoHuShiViewController alloc] init];
            [self.navigationController pushViewController:jiaoHuShiVC animated:YES];
        }
        default:
            break;
    }
}

#pragma mark - UINavigationControllerDelegate 自定义树形动画
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPush) {
        NSLog(@"自定义push跳转");
        CustomPushAnimation *customPush = [[CustomPushAnimation alloc] init];
        return customPush;
    } else  if (operation == UINavigationControllerOperationPop) {
        NSLog(@"自定义pop动画");
        NSLog(@"来自：%@，去向：%@", fromVC, toVC);
        /**
         *  <TargetViewController: 0x7fb183dc8500>，去向：<CustomTransitionAnimationViewController: 0x7fb183c941b0>
         */
        if ([fromVC isKindOfClass:[CustomTransitionAnimationViewController class]]) {
            // 说明是自己
            NSLog(@"是自己什么都不做");
        } else if ([fromVC isKindOfClass:[TargetViewController class]]) {
            CustomPopAnimation *popAnimation = [[CustomPopAnimation alloc] init];
            return popAnimation;
        }
        
    }
    return nil;
}

#pragma mark - UIViewControllerTransitioningDelegate   自定义模态动画
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    NSLog(@"present");
    CustomModelAnimaton *customModel = [[CustomModelAnimaton alloc] init];
    customModel.animationType = AnimationTypePresent;
    return customModel;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    NSLog(@"pop");
    CustomModelAnimaton *customModel = [[CustomModelAnimaton alloc] init];
    customModel.animationType = AnimationTypeDismiss;
    return customModel;
}

@end
